export default function () {
    function oemcartSearch(classStr) {
        const themeId = moi.getUrlParam("theme_id");
        const searchValue = $.trim($(classStr).val());
        if (!searchValue) {
            return false;
        }
        const URL = location.href.indexOf("/search") > -1 ? location.href : "/search";
        location.href = moi.setUrlParams(URL, { q: searchValue, page: 1, theme_id: themeId });
        return false;
    }
    window["search"] = oemcartSearch;
    window["oemsaasSearch"] = oemcartSearch;

    function oemcartMiniCart(classStr) {
        moi.onEvent("showMiniCart");
        $(".minicart").addClass("minicart_on");
        const html = $(".minicart").find(".minicart_mid").html();
        if (!html) {
            miniCart();
        }
    }

    window["oemsaasMiniCart"] = oemcartMiniCart;
    window["showMiniCart"] = oemcartMiniCart;

    /**
     * 补0
     * @param {type} num
     * @param {type} n
     * @returns {String}
     */
    function pad(num, n) {
        return num + Array(n > num ? n - ("" + num).length + 1 : 0).join(0);
    }
    /**
     * number_format
     * @param number 传进来的数,
     * @param decimals 保留的小数位,默认保留两位小数,
     * @param dec_point 小数点符号
     * @param thousands_sep 为整数位每3位间隔符号
     */
    function number_format(number, decimals, dec_point, thousands_sep) {
        var gapnum = 3;
        var str = "";

        number = number.toFixed(decimals); //格式化
        is_negative = number < 0 ? true : false;
        if (is_negative) {
            number = Math.abs(number) + "";
        }
        realnum = number.split(".")[0]; //整数位(使用小数点分割整数和小数部分)
        decimal = number.split(".")[1]; //小数位
        realnumarr = realnum.split(""); //将整数位逐位放进数组 ["1", "2", "3", "4", "5", "6"]

        //把整数部分从右往左拼接，每gapnum位添加一个thousands_sep符号
        for (var i = 1; i <= realnumarr.length; i++) {
            str = realnumarr[realnumarr.length - i] + str;
            if (i % gapnum == 0) {
                str = thousands_sep + str; //每隔gapnum位前面加指定符号
            }
        }

        //当遇到 gapnum 的倍数的时候，会出现比如 ",123",这种情况，所以要去掉最前面的 thousands_sep
        str = realnum.length % gapnum == 0 ? str.substr(1) : str;
        //重新拼接实数部分和小数位
        realnum = str + (decimals ? "" + dec_point + "" + (decimal ? pad(decimal, decimals) : pad(0, decimals)) : "");
        if (is_negative) {
            realnum = "-" + realnum;
        }
        return realnum;
    }

    //浮点数加法运算
    function FloatAdd(arg1, arg2) {
        var r1, r2, m;
        try {
            r1 = arg1.toString().split(".")[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2));
        return (arg1 * m + arg2 * m) / m;
    }

    //浮点数减法运算
    function FloatSub(arg1, arg2, decimal) {
        var r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2));
        //动态控制精度长度
        n = r1 >= r2 ? r1 : r2;
        if (decimal) {
            n = decimal;
        }
        return ((arg1 * m - arg2 * m) / m).toFixed(n);
    }

    //浮点数乘法运算
    function FloatMul(arg1, arg2, decimal) {
        var m = 0,
            s1 = arg1.toString(),
            s2 = arg2.toString();
        try {
            m += s1.split(".")[1].length;
        } catch (e) {}
        try {
            m += s2.split(".")[1].length;
        } catch (e) {}
        if (decimal) {
            return ((Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m)).toFixed(decimal);
        }
        return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
    }

    //浮点数除法运算
    function FloatDiv(arg1, arg2, decimal) {
        var t1 = 0,
            t2 = 0,
            r1,
            r2;
        try {
            t1 = arg1.toString().split(".")[1].length;
        } catch (e) {}
        try {
            t2 = arg2.toString().split(".")[1].length;
        } catch (e) {}

        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        if (decimal) {
            return Number((r1 / r2) * Math.pow(10, t2 - t1)).toFixed(decimal);
        }
        return (r1 / r2) * Math.pow(10, t2 - t1);
    }
    /**
     * 价格格式
     * @param float|string $price
     * @param integer $decimal 默认2位
     * @return float
     */
    function price_format(price, decimal = 2) {
        result = price.toFixed(decimal);
        return Number(result);
    }
    /**
     * 前台使用
     * 基准货币与其他货币之间的转换
     * @param type $price
     * @param type $tobase 是否转为基准货币
     * @return type
     */
    function currencyExchange(price, currency, decimal_num) {
        var rate = currency.exchange_rate;
        var decimal = currency.decimal_num; //小数点问题
        if (decimal_num) {
            decimal = decimal_num;
        }
        return price_format(price * rate, decimal);
    }

    // 货币格式化
    function formatMoney(number, currency, exchangeRate, appendSymbol) {
        var exchangeRate = exchangeRate || currency.exchange_rate;
        var symbolLeft = currency.symbol_left;
        var symbolRigth = currency.symbol_right;
        var currencyFormat = currency.currency_format;
        var decimalNum = currency.decimal_num;

        var decimalSeparator = ".";
        var thousandsSeparator = "'";
        switch (currencyFormat) {
            case "123,456.78":
                decimalSeparator = ".";
                thousandsSeparator = ",";
                break;
            case "123,456":
                decimalSeparator = "";
                thousandsSeparator = ",";
                decimalNum = 0;
                break;
            case "123.456,78":
                decimalSeparator = ",";
                thousandsSeparator = ".";
                break;
            case "123.456":
                decimalSeparator = "";
                thousandsSeparator = ".";
                decimalNum = 0;
                break;
            case "123'456.65":
            default:
                decimalSeparator = ".";
                thousandsSeparator = "'";
                break;
        }
        number = number || 0;
        number = price_format(number * exchangeRate, decimalNum);

        symbolLeft = symbolLeft != "" ? symbolLeft + " " : "";
        symbolRigth = symbolRigth != "" ? " " + symbolRigth : "";

        if (appendSymbol === false) {
            return number_format(number, decimalNum, decimalSeparator, thousandsSeparator);
        }

        return symbolLeft + "" + number_format(number, decimalNum, decimalSeparator, thousandsSeparator) + "" + symbolRigth;
    }

    /**
     * @description: 相加
     * @return {*}
     */
    Number.prototype.plus = function (arg) {
        var arg1 = this;
        var arg2 = arg;
        var r1, r2, m, c;
        try {
            r1 = arg1.toString().split(".")[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        } catch (e) {
            r2 = 0;
        }
        c = Math.abs(r1 - r2);
        m = Math.pow(10, Math.max(r1, r2));
        if (c > 0) {
            var cm = Math.pow(10, c);
            if (r1 > r2) {
                arg1 = Number(arg1.toString().replace(".", ""));
                arg2 = Number(arg2.toString().replace(".", "")) * cm;
            } else {
                arg1 = Number(arg1.toString().replace(".", "")) * cm;
                arg2 = Number(arg2.toString().replace(".", ""));
            }
        } else {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", ""));
        }
        return (arg1 + arg2) / m;
    };
    /**
     * @description: 相减
     * @return {*}
     */
    Number.prototype.minus = function (arg) {
        var arg1 = this;
        var arg2 = arg;
        var r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
        n = r1 >= r2 ? r1 : r2;
        return ((arg1 * m - arg2 * m) / m).toFixed(n);
    };

    /**
     * @description: 相乘
     * @return {*}
     */
    Number.prototype.times = function (arg) {
        var arg1 = this;
        var arg2 = arg;
        var m = 0,
            s1 = arg1.toString(),
            s2 = arg2.toString();
        try {
            m += s1.split(".")[1].length;
        } catch (e) {}
        try {
            m += s2.split(".")[1].length;
        } catch (e) {}
        return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
    };

    /**
     * @description: 相除
     * @return {*}
     */
    Number.prototype.modulo = function (arg) {
        var arg1 = this;
        var arg2 = arg;
        var t1 = 0,
            t2 = 0,
            r1,
            r2;
        try {
            t1 = arg1.toString().split(".")[1].length;
        } catch (e) {}
        try {
            t2 = arg2.toString().split(".")[1].length;
        } catch (e) {}
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * Math.pow(10, t2 - t1);
    };

    /* 获取购物车数量 */
    var isGetCartNumber = false;
    function addCartNum() {
        if (!isGetCartNumber) {
            var num = oemcart_cart_number.item_number;
            moi.changeCartNumber(num);
            isGetCartNumber = true;
        } else {
            moi.ajax({
                url: "/cart.json",
                type: "GET",
                dataType: "json",
                success: function (res) {
                    var num = res.data.item_number;
                    moi.changeCartNumber(num);
                }
            });
        }
    }
    function miniCart() {
        $(".minicart_form").remove();
        $.ajax({
            url: "/cart/mini",
            cache: false,
            dataType: "html",
            success: function (html) {
                $(".minicart_mid").html(html);
                moi.onEvent("miniCart");
            },
            beforeSend: function () {
                $(".minicart_top").after(
                    '<div class="minicart_ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>'
                );
            },
            complete: function () {
                $(".minicart_ajaxloading").remove();
            }
        });
    }
    function createAjaxLoading() {
        $("body").append(
            '<div class="ajaxloading"><svg t="1592222422836" class="order_btn_icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6126"><path d="M512.606821 958.898283c-246.319012 0-446.698738-200.379727-446.698738-446.698738S266.287809 65.479317 512.606821 65.479317c17.73185 0 32.117488 14.385639 32.117488 32.117488s-14.385639 32.117488-32.117488 32.117488c-210.897268 0-382.463762 171.58696-382.463762 382.484228s171.566494 382.463762 382.463762 382.463762 382.484228-171.566494 382.484228-382.463762c0-106.013499-42.384319-204.603935-119.332852-277.558503-12.859889-12.211113-13.403265-32.536021-1.212618-45.416376 12.190647-12.901845 32.536021-13.403265 45.416376-1.212618 89.870844 85.229127 139.365094 200.35926 139.365094 324.187497C959.327048 758.518556 758.925832 958.898283 512.606821 958.898283z" p-id="6127"></path></svg></div>'
        );
    }
    function removeAjaxLoading() {
        $(".ajaxloading").remove();
    }

    window['addCartNum'] = addCartNum;
    window['miniCart'] = miniCart;
    window['createAjaxLoading'] = createAjaxLoading;
    window['removeAjaxLoading'] = removeAjaxLoading;
}
